package com.cpz.rabbitmq.receiver;

import com.cpz.rabbitmq.message.DirectMessage;
import com.cpz.rabbitmq.message.FanoutMessage;
import com.cpz.rabbitmq.message.TopicMessage;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import java.io.IOException;

import static com.cpz.rabbitmq.config.RabbitConstants.*;


/**
 * 参数解析：
 * 第二个参数,true为批量确认消息（deliveryTag<=当前值）
 * channel.basicAck(deliveryTag, true);
 * 第二个参数,true为重新放回队列
 * channel.basicReject(deliveryTag, false);
 *
 * @author cpz
 * @since 2022/09/19
 */
@Slf4j
@Component
public class MessageReceiver {

    @RabbitListener(
            queuesToDeclare = @Queue(value = QUEUE_DIRECT)
    )
    @RabbitHandler
    public void direct(DirectMessage message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) throws IOException {
        log.info("【消费者】收到消息：ID：{}，交换机：{}，路由键：{}，内容：{}", message.getMessageId(), message.exchange(), message.routingKey(), message.getData());
        channel.basicAck(tag, false);
    }


    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value = "123", durable = "true"),
                    exchange = @Exchange(value = EXCHANGE_FANOUT, type = ExchangeTypes.FANOUT)
            )
    )
    @RabbitHandler
    public void fanout1(FanoutMessage message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) throws IOException {
        log.info("【消费者1】收到消息：ID：{}，交换机：{}，路由键：{}，内容：{}", message.getMessageId(), message.exchange(), message.routingKey(), message.getData());
        channel.basicAck(tag, false);
    }

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value = "456", durable = "true"),
                    exchange = @Exchange(value = EXCHANGE_FANOUT, type = ExchangeTypes.FANOUT)
            )
    )
    @RabbitHandler
    public void fanout2(FanoutMessage message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) throws IOException {
        log.info("【消费者2】收到消息：ID：{}，交换机：{}，路由键：{}，内容：{}", message.getMessageId(), message.exchange(), message.routingKey(), message.getData());
        channel.basicAck(tag, false);
    }


    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value = "456", durable = "true"),
                    exchange = @Exchange(value = EXCHANGE_FANOUT, type = ExchangeTypes.FANOUT)
            )
    )
    @RabbitHandler
    public void fanout3(FanoutMessage message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) throws IOException {
        log.info("【消费者3】收到消息：ID：{}，交换机：{}，路由键：{}，内容：{}", message.getMessageId(), message.exchange(), message.routingKey(), message.getData());
        channel.basicAck(tag, false);
    }


    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value = "AAA", durable = "true"),
                    exchange = @Exchange(value = EXCHANGE_TOPIC, type = ExchangeTypes.TOPIC),
                    key = ROUTING_KEY_TOPIC
            )
    )

    @RabbitHandler
    public void topic1(TopicMessage message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) throws IOException {
        log.info("【消费者1】收到消息：ID：{}，交换机：{}，路由键：{}，内容：{}", message.getMessageId(), message.exchange(), message.routingKey(), message.getData());
        channel.basicAck(tag, false);
    }

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value = "BBB", durable = "true"),
                    exchange = @Exchange(value = EXCHANGE_TOPIC, type = ExchangeTypes.TOPIC),
                    key = ROUTING_KEY_TOPIC
            )
    )
    @RabbitHandler
    public void topic2(TopicMessage message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) throws IOException {
        log.info("【消费者2】收到消息：ID：{}，交换机：{}，路由键：{}，内容：{}", message.getMessageId(), message.exchange(), message.routingKey(), message.getData());
        channel.basicAck(tag, false);
    }

}
